#include "backup-two.h"
#include "getmd5.h"

bool backuptwo::backup_two(const QString &path1, const QString &path2, QProgressDialog *dialog)
{
    int i = 0;
    GetFile get;
    QDir dir;
    GetMD5 md5;
    int sum = get.getnum2(path1, path2);
    if (sum > 0) {
        dialog->setFixedWidth(600);
        dialog->setWindowTitle("synchronize progress");
        dialog->setWindowModality(Qt::WindowModal);
        dialog->show();
        dialog->setRange(0, sum);
        dialog->setMinimumDuration(0);
    } else if (sum == 0){
        return true;
    }
    QFile file("../BackupAndRestore/log.txt");
    file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append);
    QFileInfoList fileInfoList = get.getAllFile(path1);
    QFileInfoList fileInfoList2 = get.getAllFile(path2);
    QList<QString> dirList = get.getDir(path1);
    QList<QString> dirList2 = get.getDir(path2);
    file.write(("start to  synchronize" + path1 + " between " + path2 + '\n').toUtf8());

    foreach (QString str, dirList) {
        QString newpath = str.replace(path1, path2);
        /*if (QDir(newpath).exists()) {
            QDir(newpath).removeRecursively();
        }*/
        if (!dir.exists(newpath)) {
            if (dir.mkpath(str.replace(path1,path2))) {
                QTime time1;
                time1.start();
                while(time1.elapsed() < 50)             //等待时间流逝5秒钟
                    QCoreApplication::processEvents();//处理事件
                i += 4096;
                dialog->setLabelText(str.replace(path1,path2));
                dialog->setValue(i);
                QDateTime *datetime=new QDateTime(QDateTime::currentDateTime());
                QString time = datetime->toString("yyyy-MM-dd hh:mm:ss ddd");
                file.write((time + "  add dir:  " + str.replace(path1, path2) + '\n').toUtf8());
            }
        }
    }

    foreach (QFileInfo info, fileInfoList) {
        QString temp;
        QString newpath = info.absoluteFilePath();
        temp = info.absoluteFilePath().replace(path1, path2);
        if (!QFile(temp).exists() ||  !QString::compare(md5.getFileMd5(temp),md5.getFileMd5(newpath))) {
            //dir.remove(temp);
            if (!QFile(temp).exists()) {
                auto local = new QThread;
                auto worker = new FileCopyer(local);
                QObject::connect(worker, &FileCopyer::finished, [](bool s) {

                });
                QObject::connect(worker, &FileCopyer::copyProgress, [](qint64 copy, qint64 total) {

                });
                QVector<QString> ve1, ve2;
                ve1.append(info.absoluteFilePath()); ve2.append(temp);
                worker->setSourcePaths(ve1) ;
                worker->setDestinationPaths(ve2);
                local->start();
                QTime time1;
                time1.start();
                while(time1.elapsed() < 50)             //等待时间流逝5秒钟
                    QCoreApplication::processEvents();//处理事件
                i += info.size();
                dialog->setLabelText(temp);
                dialog->setValue(i);
                QDateTime *datetime=new QDateTime(QDateTime::currentDateTime());
                QString time = datetime->toString("yyyy-MM-dd hh:mm:ss ddd");
                file.write((time + "  add file:  " + temp + '\n').toUtf8());
            }
       }

    }

    foreach (QString str, dirList2) {

        QString newpath = str.replace(path2, path1);
        /*if (QDir(newpath).exists()) {
            QDir(newpath).removeRecursively();
        }*/
        if (!dir.exists(newpath)) {
            if (dir.mkpath(str.replace(path2, path1))) {
                QTime time1;
                time1.start();
                while(time1.elapsed() < 50)             //等待时间流逝5秒钟
                    QCoreApplication::processEvents();//处理事件
                i += 4096;
                dialog->setLabelText(str.replace(path2,path1));
                dialog->setValue(i);
                QDateTime *datetime=new QDateTime(QDateTime::currentDateTime());
                QString time = datetime->toString("yyyy-MM-dd hh:mm:ss ddd");
                file.write((time + "  add dir:  " + str.replace(path2, path1) + '\n').toUtf8());
            }
        }

    }

    foreach (QFileInfo info, fileInfoList2) {
        QString temp;
        QString newpath = info.absoluteFilePath();
        temp = info.absoluteFilePath().replace(path2, path1);
        if (!QFile(temp).exists() ||  !QString::compare(md5.getFileMd5(temp),md5.getFileMd5(newpath))) {
            if (!QFile(temp).exists()) {
                auto local = new QThread;
                auto worker = new FileCopyer(local);
                QObject::connect(worker, &FileCopyer::finished, [](bool s) {

                });
                QObject::connect(worker, &FileCopyer::copyProgress, [](qint64 copy, qint64 total) {

                });
                QVector<QString> ve1, ve2;
                ve1.append(info.absoluteFilePath()); ve2.append(temp);
                worker->setSourcePaths(ve1) ;
                worker->setDestinationPaths(ve2);
                local->start();
                QTime time1;
                time1.start();
                while(time1.elapsed() < 50)             //等待时间流逝5秒钟
                    QCoreApplication::processEvents();//处理事件
                i += info.size();
                dialog->setLabelText(temp);
                dialog->setValue(i);
                QDateTime *datetime=new QDateTime(QDateTime::currentDateTime());
                QString time = datetime->toString("yyyy-MM-dd hh:mm:ss ddd");
                file.write((time + "  add file:  " + temp + '\n').toUtf8());
            }

        }
    }
    file.write(("finish synchronizing" + path1 + " between " + path2 + '\n').toUtf8());
    file.close();
    //QMessageBox::information(NULL, "Tip", "Synchronize Successfully!", QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
    return true;
}
